package org.hxzon.blogbackup.baidu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hxzon.util.DebugUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class FileBackuper {
    private static List<BlogImage> tmpImageInfo = new ArrayList<BlogImage>();

    public static void backupPageItem(int index) {
        try {
            Document doc = Jsoup.parse(BlogWriter.readPageInfo(index));
            Element body = doc.body();
            Elements items = body.getElementsByClass("item");
            for (Element item : items) {
//				String title = item.getElementsByClass("tit").get(0).text();
                String url = item.getElementsByClass("url").first().text();
//				System.out.println(title + "," + url);
                BlogFile file = exportItem(url, index);
                if (file.getContent() == null) {
                    DebugUtil.error("file content is null,url=" + file.getOrigUrl() + ",title" + file.getTitle().text());
                } else {
                    BlogWriter.writeItem(file);
                }
            }
        } catch (Exception e) {
            DebugUtil.error("fetch item err," + index + ":" + e.getMessage(), e);
        }
    }

    public static BlogFile exportItem(String url, int index) {
        BlogFile item = new BlogFile();
        item.setPageIndex(index);
        item.setOrigUrl(url);
        BlogImage imageInfo;
        try {
//			Document doc = Jsoup.connect(url).get();
            Document doc = BlogReader.get(url);
            Element body = doc.body();
            Element blog = body.getElementById("m_blog");//.select("div#m_blog");
//			System.out.println(blog.html());
//				System.out.println(blog);
            item.setTitle(blog.select("div.tit").first());
            item.setDate(blog.select("div.date").first());
            Element opt = blog.select("div.opt").first();
            Element category = opt.getElementsByAttributeValue("title", "查看该分类中所有文章").first();
            //类别：j·swing
            item.setCategory(category);
            Element content = blog.getElementById("blog_text");
            //update image src
            Elements images = content.getElementsByTag("img");
            for (Element image : images) {
                imageInfo = new BlogImage();
                imageInfo.setFile(item);
                String imageSrc = image.attr("src");
                if (imageSrc.contains("about:blank") || imageSrc.contains("http://imgcache.qq.com/qzone/images/b.gif")) {
                    image.remove();
                    DebugUtil.trace("remove image,file=" + item.getTitle().text() + "," + imageSrc);
                } else {
                    imageInfo.setSrc(imageSrc);
                    //use owner file and src build filepath; 
                    image.attr("src", imageInfo.getFilePath());//modify src
                    image.after("<p class='origImg'>" + imageInfo.getSrc() + "</p>");
                    //backup imageInfo for write imageInfo
                    tmpImageInfo.add(imageInfo);
                }
            }
            //after change image src
            item.setContent(content);
        } catch (Exception e) {
            DebugUtil.error("download file error," + ",url=" + item.getOrigUrl() + ":" + e.getMessage(), e);
        }
        return item;
    }

    public static void main(String args[]) throws IOException {
        //last=244
        //255-251,250-241,246,240-231,
        //230-201,200-181,180-151,162
        //150-131,130-101,
        //100-81,
        //80-51,73,50-31,44,43,30-0
        DebugUtil.clearDebugClass();
        DebugUtil.addDebugClass(FileBackuper.class.getCanonicalName());
//        for (int i = 242; i >= 242; i--) {
//            FileBackuper.backupPageItem(i);
//            BlogWriter.writeImageInfo(tmpImageInfo, i);
//            tmpImageInfo.clear();
//            DebugUtil.info(i + ",ok");
//        }
    }
}
